/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.helper;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.DurationObservation;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Event;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.helper.GraphEdgeHelper;
import com.cea.papyrus.core.helper.IGraphEdgeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.sequence.ui.dialogs.ChooseDurationEvent;
import com.cea.papyrus.diagram.sequence.ui.dialogs.ChooseEventofLifeLineDialog;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * 
 */
public class EdgeDurationObservationHelper extends GraphEdgeHelper implements IGraphEdgeHelper{

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.IGraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode, com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */	
	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		if (source==target){return false;}// generalization not on the same object
		if (source instanceof DurationObservation&& target== null) {// they are  classifiers
			return true;
		}

		if (source instanceof DurationObservation&& target instanceof Lifeline) {// they are  classifiers
			return true;
		}
		return false;

	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		Element eltSource=null;

		return true;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#createGraphEdge(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * 
	 * 
	 * @param umlElement 
	 * @param diagram 
	 * 
	 * @return 
	 */
	@Override
	public ArrayList createGraphEdge(Element umlElement, Diagram diagram){

		return new ArrayList();
	}

	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	public  org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		return null;		
	}


	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean canCreateUMLElement(Element umlParent) {
		return false;
	}


	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean addUMLElement(Element element, Element umlParent) {
		return false;
	}


	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean removeUMLElement(Element element, Element umlParent) {
		return false;
	}

	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	public boolean connect(Element source, Element target, Element link) {
		ChooseDurationEvent cde= new ChooseDurationEvent();
		cde.open();
		int select=cde.getSelection();

		ChooseEventofLifeLineDialog celd= new ChooseEventofLifeLineDialog(new Shell(),(Lifeline)target);
		celd.open();
		Event event= celd.getResult();
		NamedElement[] eventarray= new Event[2];
		if(((DurationObservation)source).getEvents().size()>0){
			eventarray[0]=((DurationObservation)source).getEvents().get(0);
		}
		if(((DurationObservation)source).getEvents().size()>1){
			eventarray[1]=((DurationObservation)source).getEvents().get(1);
		}
		if(select==ChooseDurationEvent.START){
			eventarray[0]=event;
		}
		else if(select==ChooseDurationEvent.END){
			eventarray[1]=event;
		}
		((DurationObservation)source).getEvents().clear();
		for( int i=0; i< eventarray.length;i++){
			if(eventarray[i]!=null){
				((DurationObservation)source).getEvents().add((Event)eventarray[i]);}
		}
		return true;
	}

	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	public boolean deconnect(Element source, Element target, Element link) {

		if(source instanceof Constraint){
			Constraint co= (Constraint)source;
			co.getConstrainedElements().remove(target);

		} else if (source instanceof Comment) {
			Comment co= (Comment) source;
			co.getAnnotatedElements().remove(target);

		} else if (target instanceof Constraint) {
			Constraint co= (Constraint)target;
			co.getConstrainedElements().remove(source);

		} else if (target instanceof Comment) {
			Comment co= (Comment) target;
			co.getAnnotatedElements().remove(source);

		} else {
			Debug.log(new Exception("Unexpected type."));
			return false;
		}

		return true;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public String getBaseString() {
		return "Link"; //$NON-NLS-1$
	}

}
